<?php

App::uses('AppController', 'Controller');


class DashController extends AppController
{
    public function index($dummy, $teamId)
    {
        $this->loadModel('User');
        $this->loadModel('Team');
        $this->loadModel('TeamUser');
        $this->loadModel('Categorization');
        $this->Team->id = $teamId;
        $team = $this->Team->read();
        $teamUsers = $this->TeamUser->getTeamWithDetails($teamId);
        $teamUsers90 = $this->TeamUser->getTeamWithDays($teamId, 90);
        $teamUsers270 = $this->TeamUser->getTeamWithDays($teamId, 270);
        $teamUsers450 = $this->TeamUser->getTeamWithDays($teamId, 450);
        $levels = $this->Team->getTotalUsersByLevel($teamId);
        $certifications = $this->Team->getTotalCertifications($teamId);
        $totalAsociates = $this->User->getTotalAsociates();
        $appBosch = new stdClass();
        $appBosch->teamUsers = $teamUsers;
        $appBosch->teamUsers90 = $teamUsers90;
        $appBosch->teamUsers270 = $teamUsers270;
        $appBosch->teamUsers450 = $teamUsers450;
        $appBosch->team = $team['Team'];
        $appBosch->levels = $levels;
        $appBosch->totalUsers = count($teamUsers);
        $appBosch->certifications = $certifications[0];
        $appBosch->totalAsociates = $totalAsociates;
        $appBosch->courses = $this->processLevels($appBosch->totalUsers, $levels);
        $this->getTotalAsociatesWithoutLevel($appBosch, $team);
        $this->set('appBosch', $appBosch);
        $this->set('title', 'Reporte Dash');
        $this->set('description', $team['Team']['name']);
    }

    private function processLevels($totalUsers, $levels)
    {
        $courses = array();
        foreach ($levels as $key => $level) {
            $arraySlice = array_slice($levels, $key);
            $sum = array_sum(Set::classicExtract($arraySlice, '{n}.value'));
            $level['value'] = $sum / $totalUsers;
            $level['name'] = $level['category'];
            $courses[] = $level;
        }
        return $courses;
    }

    private function getTotalAsociatesWithoutLevel($appBosch, $team)
    {
        $lineId = $team['Team']['line_id'];
        $teamId = $team['Team']['id'];
        $lowLevels = $this->Categorization->getLowLevels('Multihabilidad inicial', $lineId);
        $lowLevels = Set::classicExtract($lowLevels, '{n}.id');
        $appBosch->withoutInicial = $this->Team->getByTeamAndDaysAndLevel($teamId, 90, $lowLevels);
        $lowLevels = $this->Categorization->getLowLevels('Multihabilidad intermedio', $lineId);
        $lowLevels = Set::classicExtract($lowLevels, '{n}.id');
        $appBosch->withoutIntermedio = $this->Team->getByTeamAndDaysAndLevel($teamId, 270, $lowLevels);
        $lowLevels = $this->Categorization->getLowLevels('Multihabilidad avanzado', $lineId);
        $lowLevels = Set::classicExtract($lowLevels, '{n}.id');
        $appBosch->withoutAvanzado = $this->Team->getByTeamAndDaysAndLevel($teamId, 450, $lowLevels);
    }
}